package org.dgm.dgmJar.mailUtil;

import java.util.Date;
import java.util.Map;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.dgm.dgmJar.propertyUtil.DCommProperty;



/**
 * 现在用了MimeMessage这个作为msg,
 * 只可传文本，
 * dgmJar中的DCommMailTest可为例子
 * DCommMail 
 *  
 * @author：邓国明
 * @Aug 22, 2012 3:51:54 PM 
 * @version 1.0
 */
public class DCommMail {
	
	private static DCommMail dcommmail;
	
	private static String propPath;
	
	/**
	 * 整个mail的etc
	 */
	private Properties etcProperties;
	
	/**
	 * Mail的session
	 */
	private Session session;
	
	
	private DCommMail(){
		
	}
	
	/**
	 * 强制单例，默认路径为
	 * getInstance
	 *
	 * @return DCommMail
	 * @Exception
	 * @author：邓国明
	 * @Aug 22, 2012 9:51:06 AM 
	 * @update:
	 * @Aug 22, 2012 9:51:06 AM
	 */
	public static DCommMail getInstance(){
		return getInstance("/tDCommMail.properties");
	}
	
	
	public static DCommMail getInstance(String propertiesPath){
		if(propertiesPath.equals(propPath)&&dcommmail!=null){ //路径相同,说明已有了dcommmail
			return dcommmail;
		}
		//路径不相同，直接生成，然后..覆盖
		propPath=propertiesPath;
		dcommmail = new DCommMail();
		dcommmail.etcProperties = DCommProperty.getPropertyInstance(propertiesPath);
		return dcommmail;
	}
	
	
	/**
	 * 
	 * send
	 * 
	 *  msg.setSubject("mail", "utf-8");
     *	String txt = "你妹啊?";
	 *	msg.setText(txt, "utf-8");
	 *
	 * @param msg
	 * @param toListString void
	 * @Exception
	 * @author：邓国明
	 * @Aug 22, 2012 3:37:01 PM 
	 * @update:
	 * @Aug 22, 2012 3:37:01 PM
	 */
	public void send(MimeMessage msg,String toListString){
		
		try {
			InternetAddress toList[] = InternetAddress.parse(toListString,false);
			msg.addRecipients(MimeMessage.RecipientType.TO, toList);
			
			Transport transport = session.getTransport("smtp");
			transport.connect(etcProperties.getProperty("postServer"), etcProperties.getProperty("userId"), etcProperties.getProperty("userPassword"));//***这里加上密码
			transport.sendMessage(msg, toList);
			
			System.out.println("向"+toListString+"发送了数据");
			
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * 得到格式化的msg,可将标题和正文加入
	 * getMsg
	 *
	 * @return
	 * @throws Exception MimeMessage
	 * @Exception
	 * @author：邓国明
	 * @Aug 22, 2012 3:23:02 PM 
	 * @update:
	 * @Aug 22, 2012 3:23:02 PM
	 */
	public  MimeMessage getMsg() throws Exception {
		Properties props = System.getProperties();
		props.put("mail.smtp.host", etcProperties.getProperty("mail.smtp.host"));
		props.put("mail.smtp.auth", etcProperties.getProperty("mail.smtp.auth"));
		
		session = Session.getDefaultInstance(props, null);
		MimeMessage msg = new MimeMessage(session);
		InternetAddress fromAddress = new InternetAddress(etcProperties.getProperty("fromAddress"));
		msg.setFrom(fromAddress);
		msg.setSentDate(new Date());
		return msg;
	}
	
}
